package com.example.replacebooks.service;

import com.example.replacebooks.common.JwtTokenUtils;
import com.example.replacebooks.entity.Params;
import com.example.replacebooks.entity.User;
import com.example.replacebooks.exception.CustomException;
import com.example.replacebooks.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    public List<User> findAll(){
        return userMapper.selectAll();
    }

    public PageInfo<User> findBySearch(Params params){
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页查询
        List<User> list = userMapper.findBySearch(params);
        return PageInfo.of(list);
    }

    public void add(User user) {
        // 1. 用户id一定要有，否则不让新增
        if (user.getUserid() == null){
            throw new CustomException("用户id不能为空");
        }
        // 2. 进行重复性判断，同一id的管理员不允许新增
        User userid = userMapper.findByUserid(user.getUserid());
        if (userid != null){
            // 该id已存在
            userMapper.updateByuserid(user);
        }else{
            if (user.getRole() == null ){
                user.setRole(0);
            }
            userMapper.insertSelective(user);
        }
    }

    public void delete(Integer userid) {
        userMapper.deleteByPrimaryKey(userid);
    }

    public User login(User user) {
        // 1. 非空判断
        if (user.getUsername() == null){
            throw new CustomException("用户名不能为空");
        }
        if (user.getPassword() == null){
            throw new CustomException("密码不能为空");
        }
        if (user.getRole() == null){
            throw new CustomException("请选择登录权限（管理员、普通用户）");
        }
        // 2. 从数据库里面根据用户名、密码和权限查询对应的用户信息
        User user1 = userMapper.findUseridAndPasswordAndRole(user.getUsername(),user.getPassword(),user.getRole());
        if (user1 == null){
            //查不出来账号、密码或者权限错误
            throw new CustomException("用户名、密码输入错误 或 权限选择错误");
        }
        String userid = user1.getUserid().toString();
        //查询出来结果,生成该用户对应的token，然后跟着user一起返回前台
        String token = JwtTokenUtils.genToken(userid, user.getPassword());
        user1.setToken(token);
        return user1;
    }

    public void register(User user) {
        // 1. 用户账号、密码和用户名一定要有，否则不让新增
        if (user.getUsername() == null){
            throw new CustomException("用户账号不能为空");
        }
        if (user.getPassword() == null){
            throw new CustomException("用户密码不能为空");
        }
        if (user.getName() == null){
            throw new CustomException("用户名不能为空");
        }
        // 2. 进行重复性判断，同一用户账号的管理员不允许新增
        User userid = userMapper.findByUsername(user.getUsername());
        if (userid != null){
            // 该用户账号已存在
            throw new CustomException("用户账号已存在，请重新输入！");
        }
        if (user.getRole() == null ){
            user.setRole(0);
        }
        userMapper.insertSelective(user);
    }

    public User findById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    public User findByUserid(Integer userid) {
        User list = userMapper.findByUserid(userid);
        return list;
    }
}
